﻿using System;
using System.Collections.Generic;
using CookComputing.XmlRpc;
//
using PerceptiveMCAPI.Types;
using PerceptiveMCAPI.Types.Internal;
//
namespace PerceptiveMCAPI.Methods
{
   public class listBatchSubscribe : Api_BaseExecute
   {

      private new listBatchSubscribeInput Input { get; set; }
      private listBatchSubscribeOutput Output { get; set; }
      // =============================================== constructors
      public listBatchSubscribe() { }
      // ---------
      public listBatchSubscribe( listBatchSubscribeInput input ) { Input = input; }
      // =============================================== Execute
      public listBatchSubscribeOutput Execute()
      {
         // empty constructor & Execute() is bad
         if ( Input == null )
            throw new ArgumentException( "Input class not provided" );
         // do the deed
         Output = Execute( Input );
         return Output;
      }
      // ---------------------------------------------
      public listBatchSubscribeOutput Execute( listBatchSubscribeInput input )
      {
         Input = input;
         Output = new listBatchSubscribeOutput( Input );
         ValidationIsOK = true;
         //------------------------- validate it
         if ( Input.api_Validate )
         {
            Output.api_ValidatorMessages = ValidateInput.Validate( Input );
            ValidationIsOK = ValidateInput.IsOK( Output.api_ValidatorMessages );
         }
         //------------------------- do it
         if ( ValidationIsOK )
         {
            ApiRun( Input, input.parms.apikey );  // execute API & format output
         }
         // ---
         return Output;
      }

      #region ================================================================= API Calls
      // ---------------------------------------------------------- XmlRpc
      protected override void ExecuteXmlRpc()
      {
         try
         {
            XmlRpcStruct batch = FormatBatchSubscribe( Input.parms.batch );

            x_listBatchSubscribe rpcResult = apiXmlRpc.api.listBatchSubscribe(
               Input.parms.apikey, Input.parms.id, batch, Input.parms.double_optin, Input.parms.update_existing, Input.parms.replace_interests );
            Output.Format( rpcResult );
         }
         catch ( XmlRpcFaultException fex )
         {
            Output.api_ErrorMessages.Add( apiException.FormatError( fex ) );
         }
         catch ( Exception ex )
         {
            Output.api_ErrorMessages.Add( apiException.FormatError( ex ) );
         }
      }
      // -----------------------------
      private XmlRpcStruct FormatBatchSubscribe( List<Dictionary<string, object>> input )
      {
         int i = -1;
         XmlRpcStruct batch = new XmlRpcStruct();

         foreach ( Dictionary<string, object> entry in input )
         {
            i++;
            XmlRpcStruct batchentry = apiXmlRpc.FormatMemberMergeVars( entry );
            batch.Add( i.ToString(), batchentry );
         }
         return batch;
      }
      // ---------------------------------------------------------- Serial Post
      protected override void ExecuteSerialPost()
      {
         try
         {
            Dictionary<string, string> dict = new Dictionary<string, string>();
            dict.Add( "apikey", Input.parms.apikey );
            dict.Add( "id", Input.parms.id );
            dict.Add( "double_optin", Input.parms.double_optin.ToString() );
            dict.Add( "update_existing", Input.parms.update_existing.ToString() );
            dict.Add( "replace_interests", Input.parms.replace_interests.ToString() );

            int bi = -1;
            foreach ( Dictionary<string, object> entry in Input.parms.batch )
            {
               bi++;
               Dictionary<string, string> entry_fmt = apiSerial.FormatMemberMergeVars( entry );
               foreach ( KeyValuePair<string, string> mv in entry_fmt )
               {
                  dict.Add( string.Format( "batch[{0}]{1}", bi, mv.Key ), mv.Value );
               }
            }
            // -- do it
            string api_Request = "";
            string api_Response = apiSerial.ProcessRequest( Input, dict, out api_Request );
            Output.Format( api_Request, api_Response );
         }
         catch ( Exception ex )
         {
            Output.api_ErrorMessages.Add( apiException.FormatError( ex ) );
         }
      }

      #endregion
   }
}
